﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Linq;
using System.Text;

using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Data;

namespace RelateImageDocALS
{
    public class Grafico : IDisposable
    {
        

        #region Properties ...


            private Image _Imagen;
            public Image Imagen
            {
                get { return _Imagen; }
                set { _Imagen = value; }

            }

            private int _Ancho;
            public int Ancho
            {
                get { return _Ancho; }
                set { _Ancho = value; }

            }

            private int _Alto;
            public int Alto
            {
                get { return _Alto; }
                set { _Alto = value; }

            }

            private int _Resolucion;
            public int Resolucion
            {
                get { return _Resolucion; }
                set { _Resolucion = value; }

            }

            private int _SizeHorizontalPercent;
            public int SizeHorizontalPercent
            {
                get { return _SizeHorizontalPercent; }
                set { _SizeHorizontalPercent = value; }

            }

            private int _SizeVerticalPercent;
            public int SizeVerticalPercent
            {
                get { return _SizeVerticalPercent; }
                set { SizeVerticalPercent = value; }

            }
    #endregion

        #region Constructores ...
            public Grafico()
            {}

            public Grafico(Image imgImagen,
                            int intAncho,
                            int intAlto,
                            int intresolucion)
            {
                this._Imagen = imgImagen ;
                this._Ancho = intAncho;
                this._Alto = intAlto;
                this._Resolucion = intresolucion ;
            }


            public Grafico(int intSizeHorizontalPercent,
                            int intSizeVerticalPercent)
            {
                this._SizeHorizontalPercent = intSizeHorizontalPercent;
                this._SizeVerticalPercent = intSizeVerticalPercent;

            }

    #endregion

        MemoryStream imagenMemoryStream = new MemoryStream();
        private IntPtr handle;
        Component component = new Component();
        private DataSet dataSetDisposable = new DataSet();

        private bool disposed = false;

        public Image Redimensionar(Image Imagen, int Ancho, int Alto, int resolucion)
        {
            //Bitmap sera donde trabajaremos los cambios
            using (Bitmap imagenBitmap = new Bitmap(Ancho, Alto, PixelFormat.Format32bppRgb))
            {
                imagenBitmap.SetResolution(resolucion, resolucion);
                //Hacemos los cambios a ImagenBitmap usando a ImagenGraphics y la Imagen Original(Imagen)
                //ImagenBitmap se comporta como un objeto de referenciado
                using (Graphics imagenGraphics = Graphics.FromImage(imagenBitmap))
                {
                    imagenGraphics.SmoothingMode = SmoothingMode.AntiAlias;
                    imagenGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    imagenGraphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
                    imagenGraphics.DrawImage(Imagen, new Rectangle(0, 0, Ancho, Alto), new Rectangle(0, 0, Imagen.Width, Imagen.Height), GraphicsUnit.Pixel);
                    //todos los cambios hechos en imagenBitmap lo llevaremos un Image(Imagen) con nuevos datos a travez de un MemoryStream
                    //MemoryStream imagenMemoryStream = new MemoryStream();
                    imagenBitmap.Save(imagenMemoryStream, ImageFormat.Jpeg);
                    Imagen = Image.FromStream(imagenMemoryStream);

                    //imagenMemoryStream.Dispose();
                }
            }

            return Imagen;
        }

        public Image Redimensionar(Image image, int SizeHorizontalPercent, int SizeVerticalPercent)
        {
            //Obntenemos el ancho y el alto a partir del porcentaje de tamaño solicitado
            int anchoDestino = image.Width * SizeHorizontalPercent / 100;
            int altoDestino = image.Height * SizeVerticalPercent / 100;
            //Obtenemos la resolucion original 
            int resolucion = Convert.ToInt32(image.HorizontalResolution);

            return this.Redimensionar(image, anchoDestino, altoDestino, resolucion);
        }



        #region IDisposable Members

        public void Dispose()
        {
            imagenMemoryStream.Dispose();
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called.
            if(!this.disposed){
                // If disposing equals true, dispose all managed // and unmanaged resources
                if(disposing)
                {
                    // Dispose managed resources.
                    component.Dispose();
                    this.dataSetDisposable.Dispose();
                }

                // Call the appropriate methods to clean up // unmanaged resources here.// If disposing is false, // only the following code is executed.
                CloseHandle(handle);
                handle = IntPtr.Zero;
                
            }
            disposed = true;
        }

        [System.Runtime.InteropServices.DllImport("Kernel32")]
        private extern static Boolean CloseHandle(IntPtr handle);

        // Use C# destructor syntax for finalization code.// This destructor will run only if the Dispose method // does not get called.// It gives your base class the opportunity to finalize.// Do not provide destructors in types derived from this class.
        ~Grafico()      
        {
            // Do not re-create Dispose clean-up code here.// Calling Dispose(false) is optimal in terms of// readability and maintainability.
            Dispose(false);
        }

        #endregion


    }




}
